package karileet.meeting.profile;

import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.content.res.Resources;
import android.support.v4.app.NotificationCompat;
import android.support.v4.app.TaskStackBuilder;
import android.util.Log;
import android.widget.Toast;
import karileet.meeting.profile.activity.MainActivity;
import karileet.meeting.profile.receiver.NotificationBroadcastReceiver;

/**
 * Class notifies user about enabling (notification in notification bar) or
 * disabling app (toast)
 *
 * @author markus karileet
 */
public class ProfileNotifications {
    private static final int NOTIFICATION_ID = 1;
    private static String ns = Context.NOTIFICATION_SERVICE;
    private Context ctx;

    public ProfileNotifications(Context ctx) {
        this.ctx = ctx;
    }

    /**
     * Method removes created notification
     */
    public void cancelNotification() {
        Log.d(ProfileConf.APP, "Starting 'cancelNotification' method");
        NotificationManager mNotificationManager = (NotificationManager) ctx.getSystemService(ns);
        mNotificationManager.cancel(NOTIFICATION_ID);

        // do a toast notification for user
        CharSequence text = ctx.getResources().getString(
                R.string.profile_deactivated_toast);
        int duration = Toast.LENGTH_LONG;
        Toast toast = Toast.makeText(ctx, text, duration);
        toast.show();
    }

    /**
     * Method creates notification that profile is active
     */
    public void initiateNotification() {
        Log.d(ProfileConf.APP, "Starting 'initiateNotification' method");
        CharSequence contentTitle = ctx.getResources().getString(
                R.string.profile_active_ticker);
        CharSequence contentText = ctx.getResources().getString(
                R.string.profile_active_description);
        CharSequence tickerText = ctx.getResources().getString(
                R.string.profile_active_ticker);
        NotificationCompat.Builder mBuilder =
                new NotificationCompat.Builder(ctx)
                        .setSmallIcon(R.drawable.ic_launcher)
                        .setTicker(tickerText)
                        .setContentTitle(contentTitle)
                        .setContentText(contentText)
                        .setOngoing(true);

        Intent intent = new Intent(ctx, NotificationBroadcastReceiver.class);
        intent.setAction(ProfileConf.DISABLE);
        Resources res = ctx.getResources();
        PendingIntent pIntent = PendingIntent.getBroadcast(ctx, 0, intent, 0);
        mBuilder.addAction(android.R.drawable.ic_lock_power_off, res.getString(R.string.disableProfile), pIntent);

        Intent vintent = new Intent(ctx, NotificationBroadcastReceiver.class);
        vintent.setAction(ProfileConf.TOGGLE);
        PendingIntent pIntent2 = PendingIntent.getBroadcast(ctx, 0, vintent, 0);
        mBuilder.addAction(android.R.drawable.ic_lock_silent_mode_off, res.getString(R.string.toggleVibrate), pIntent2);

        // Creates an explicit intent for an Activity in your app
        Intent resultIntent = new Intent(ctx, MainActivity.class);
        // The stack builder object will contain an artificial back stack for the
        // started Activity.
        // This ensures that navigating backward from the Activity leads out of
        // your application to the Home screen.
        TaskStackBuilder stackBuilder = TaskStackBuilder.create(ctx);
        // Adds the back stack for the Intent (but not the Intent itself)
        stackBuilder.addParentStack(MainActivity.class);
        // Adds the Intent that starts the Activity to the top of the stack
        stackBuilder.addNextIntent(resultIntent);
        PendingIntent resultPendingIntent =
                stackBuilder.getPendingIntent(
                        0,
                        PendingIntent.FLAG_UPDATE_CURRENT
                );
        mBuilder.setContentIntent(resultPendingIntent);
        NotificationManager mNotificationManager =
                (NotificationManager) ctx.getSystemService(ns);
        // mId allows you to update the notification later on.
        mNotificationManager.notify(NOTIFICATION_ID, mBuilder.build());
    }
}
